package com.wxh.jdk8;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: ListTest
 * @Description: 集合的交集、差集、并集、去重并集
 * @Author wxh
 * @Date: 2019/9/25 10:03
 * @Version V1.0.0
 * @Since 1.8
 */
public class ListTest {
    public static void main(String[] args) {
        List<String> list1 = Arrays.asList("11","22","33");
        List<String> list2 = Arrays.asList("33","44","55");

        // 交集
        List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(Collectors.toList());
        System.out.println("------------交集---------------");
        intersection.stream().forEach(System.out::println);

        // 差集 (list1 - list2)
        List<String> different1 = list1.stream().filter(item -> !list2.contains(item)).collect(Collectors.toList());
        System.out.println("------------差集 (list1 - list2)---------------");
        different1.stream().forEach(System.out::println);

        // 差集 (list2 - list1)
        List<String> different2 = list2.stream().filter(item -> !list1.contains(item)).collect(Collectors.toList());
        System.out.println("------------差集 (list2 - list1)---------------");
        different2.stream().forEach(System.out::println);

        // 并集
        List<String> listAll = list1.stream().collect(Collectors.toList());
        listAll.addAll(list2);
        System.out.println("------------并集---------------");
        listAll.stream().forEach(System.out::println);

        // 去重并集
        List<String> listAllDistinct = listAll.stream().distinct().collect(Collectors.toList());
        System.out.println("------------去重并集---------------");
        listAllDistinct.stream().forEach(System.out::println);
    }
}
